package com.wujie.hello.spring.cloud.alibaba.nacos.oauth.authorization.config;

import com.wujie.hello.spring.cloud.alibaba.nacos.oauth.authorization.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.web.authentication.logout.LogoutFilter;

/**
 * @author wujie
 * @version 1.0.0:
 * @title WebSecurityConfiguration
 * @Description TODO
 * @dateTime 2020/9/8 16:53
 * @Copyright 2020-2021
 */
@Configuration
@EnableWebSecurity()
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;
    @Autowired
    private IgnoreLogoutFilter ignoreLogoutFilter;
    /**
     * 配置密码加密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("secret");
        System.out.println(encode);
        boolean matches = bCryptPasswordEncoder.matches("secret", "$10$IAyGJKz38qyXU1w/tK0fjOCWTRa8r1pJv/jZFX/8O6qaEhemGURsi");
        boolean matches1 = bCryptPasswordEncoder.matches("secret", encode);
        System.out.println(matches);
        System.out.println(matches1);
    }

    /**
     *  安全请求配置，这里配置的是Security的部分，请求全部通过，安全拦截在资源服务器配置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置允许的请求以及跨域问题
        http
                .formLogin().loginPage("/login")
                .permitAll()
                .and().authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable().cors()
                .and().addFilterAt(ignoreLogoutFilter, LogoutFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置获取用户信息以及加密方式
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //配置忽略一些静态资源
        web.ignoring().antMatchers("/css/**","/static/**");
    }
}
